package com.cqie.secondhandtransaction.controller;

import com.cqie.secondhandtransaction.DTO.*;
import com.cqie.secondhandtransaction.controller.params.ProductTypeParam;
import com.cqie.secondhandtransaction.entity.ProductInfo;
import com.cqie.secondhandtransaction.entity.ProductType;
import com.cqie.secondhandtransaction.model.Result;
import com.cqie.secondhandtransaction.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gdy
 * @since 2025-02-10
 */
@RestController
@RequestMapping("/product")
@Api(value = "商品管理", tags = {"商品管理"})
public class ProductInfoController extends BaseController{

    @Autowired
    private ProductInfoService productInfoService;

    @PostMapping("/list")
    @ApiOperation(value = "根据类别获取商品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<DataList<ProductInfo>> getProductByTypeIds(@RequestBody ProductTypeParam param) {
        return response(productInfoService.getProductByTypeIds(param));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询商品详情", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<ProductInfoDTO> getProductDetail(@RequestParam(value = "id") Integer id){
        return response(productInfoService.getProductDetail(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加/更新商品", httpMethod = "POST", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<Void> saveProduct(
            @Validated @ModelAttribute ProductInfoAddOrEditDTO addDTO,
            @RequestParam(value = "pictures") List<MultipartFile> pictures) {

        productInfoService.saveProduct(addDTO, pictures);
        return response();
    }

    @PostMapping("/audit")
    @ApiOperation(value = "管理员审核商品（发布商品）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")
    public Result<Void> auditProduct(@RequestBody @Valid AuditProductDTO dto) {
        productInfoService.auditProduct(dto);
        return response();
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除商品", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")
    public Result<Void> deleteProduct(@RequestParam(value = "id") Integer id) {
        productInfoService.deleteProduct(id);
        return response();
    }

    @PostMapping("/status")
    @ApiOperation(value = "根据状态获取商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200, message = "ok", response = Result.class)
    public Result<DataList<ProductInfo>> getProductInfoByStatus(@RequestBody @Validated StatusDTO param) {
        return response(productInfoService.getUserProductInfo(param));
    }

}
